package com.nd.android.musicboxlibrary.ui.vip;

import java.lang.reflect.Method;

import android.content.Context;

import com.nd.android.musicboxlibrary.business.comm.ResponseCallback;

public class VipOperationWrapper {

	private static final String CLAZZ_NAME_VIPMANAGER = "com.nd.android.starapp.ui.backpack.common.VipInfoManager";
	
	/**
	 * s获取单例
	 * @author 			DingXiaohui
	 * @date 				2014-11-19 上午11:05:48
	 * @description 	
	 * @return
	 */
	private static Object getManager(Context context) {
		try {
			Class<?> clazz = Class.forName( CLAZZ_NAME_VIPMANAGER );
			Method method = clazz.getDeclaredMethod( "getInstance", new Class[] { Context.class } );
			Object mgr = method.invoke( null, new Object[] { context } );
			return mgr;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 判断 用户是否为VIP
	 * @author 			DingXiaohui
	 * @date 				2014-11-19 上午11:11:26
	 * @description 	
	 * @return
	 */
	public static boolean isVip(Context context) {
		try {
			Class<?> clazz = Class.forName( CLAZZ_NAME_VIPMANAGER );
			Method method = clazz.getDeclaredMethod( "isVip", (Class[])null );
			Object mgr = getManager( context );
			return (Boolean) method.invoke( mgr, (Object[])null );
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 异步获取VIP信息（到期时间）
	 * @author 			DingXiaohui
	 * @date 				2014-11-19 上午11:05:59
	 * @description 	
	 * @param context
	 */
	public static void asyncUpdateVipInfo(Context context) {
		try {
			Class<?> clazz = Class.forName( CLAZZ_NAME_VIPMANAGER );
			Method method = clazz.getDeclaredMethod( "asyncUpdateVipInfo", new Class[] { Context.class } );
			Object mgr = getManager( context );
			method.invoke( mgr, new Object[] { context } );
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 异步获取VIP信息（到期时间）
	 * @author 			DingXiaohui
	 * @date 				2014-11-19 上午11:06:31
	 * @description 	如果网络异常，则回调不执行
	 * @param context
	 * @param callback
	 */
	public static void asyncUpdateVipInfo(Context context, Runnable callback) {
		try {
			Class<?> clazz = Class.forName( CLAZZ_NAME_VIPMANAGER );
			Method method = clazz.getDeclaredMethod( "asyncUpdateVipInfo", new Class[] { Context.class, Runnable.class } );
			Object mgr = getManager( context );
			method.invoke( mgr, new Object[] { context, callback } );
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取VIP到期时期
	 * @author 			DingXiaohui
	 * @date 				2014-11-19 上午11:06:56
	 * @description 	
	 * @return
	 */
	public static long getVipEndTime(Context context) {
		try {
			Class<?> clazz = Class.forName( CLAZZ_NAME_VIPMANAGER );
			Method method = clazz.getDeclaredMethod( "getVipEndTime", (Class[])null );
			Object mgr = getManager( context );
			return (Long) method.invoke( mgr, (Object[])null );
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}
	
	/**
	 * 判断 VIP到期时间是否在一周之内
	 * @author 			DingXiaohui
	 * @date 				2014-11-19 上午11:08:05
	 * @description 	
	 * @return
	 */
	public static boolean isOutOfDate(Context context) {
		try {
			Class<?> clazz = Class.forName( CLAZZ_NAME_VIPMANAGER );
			Method method = clazz.getDeclaredMethod( "isOutOfDate", (Class[])null );
			Object mgr = getManager( context );
			return (Boolean) method.invoke( mgr, (Object[])null );
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 标记 VIP到期提示拦截
	 * @author 			DingXiaohui
	 * @date 				2014-11-20 下午7:45:28
	 * @description 	
	 * @param context
	 * @param needIntercept
	 * @return
	 */
	public static boolean markVipNotifyIntercepted(Context context, boolean needIntercept) {
		try {
			Class<?> clazz = Class.forName( CLAZZ_NAME_VIPMANAGER );
			Method method = clazz.getDeclaredMethod( "markVipNotifyIntercepted", new Class[] { Context.class, Boolean.TYPE } );
			Object mgr = getManager( context );
			return (Boolean) method.invoke( mgr, new Object[] { context, needIntercept } );
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	
	private static final String CLAZZ_NAME_VIPHTTPUTILS = "com.nd.android.starapp.business.comm.VipHttpUtils";
	
	/**
	 * 购买VIP
	 * @author 			DingXiaohui
	 * @date 				2014-11-19 上午11:11:36
	 * @description 	
	 * @param context
	 * @param type
	 * @param guid
	 * @param callback
	 */
	public static void renewVip(Context context, int type, String guid, ResponseCallback callback) {
		try {
			Class<?> clazz = Class.forName( CLAZZ_NAME_VIPHTTPUTILS );
			Method method = clazz.getDeclaredMethod( "renewVip", new Class[] { 
					Context.class, Integer.TYPE, String.class, ResponseCallback.class } );
			Object mgr = getManager( context );
			method.invoke( mgr, new Object[] { context, type, guid, callback } );
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取 VIP当前星钻数量
	 * @author 			DingXiaohui
	 * @date 				2014-11-19 下午5:06:37
	 * @description 	
	 * @param context
	 * @param callback
	 */
	public static void getVipDiamondRemain(Context context, ResponseCallback callback) {
		try {
			Class<?> clazz = Class.forName( CLAZZ_NAME_VIPHTTPUTILS );
			Method method = clazz.getDeclaredMethod( "getVipDiamondRemain", new Class[] { 
					Context.class, ResponseCallback.class } );
			Object mgr = getManager( context );
			method.invoke( mgr, new Object[] { context, callback } );
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
